package top.hidoctor.auscultation.feature.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tang.util.AppUtil;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.adapter.ShaixuanAdapter;
import top.hidoctor.auscultation.adapter.ZhongcaiAdapter;
import top.hidoctor.auscultation.base.BaseActivity;
import top.hidoctor.auscultation.bean.ArbitrationBean;
import top.hidoctor.auscultation.bean.ShaixuanBean;
import top.hidoctor.auscultation.net.c.ArbitrationListContract;
import top.hidoctor.auscultation.net.p.ArbitrationListPresenter;
import top.hidoctor.auscultation.net.req.ArbitrationReq;
import top.hidoctor.auscultation.widget.Player;

public class ZhongcaiListActivity extends BaseActivity implements ArbitrationListContract.View, View.OnClickListener {

    private RecyclerView recycler, rcCondition;
    private ZhongcaiAdapter adapter;
    private SwipeRefreshLayout refreshLayout;
    private EditText editSearch;
    private TextView tvShaixuan;
    private ArbitrationListPresenter presenter;
    private ArbitrationReq req;
    private List<ShaixuanBean> shaixuanBeanList = new ArrayList<>();
    private ShaixuanAdapter shaixuanAdapter;
    private DrawerLayout drawer;
    private Button btnChongzhi, btnConfirm;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_zhongcai_list);
        presenter = new ArbitrationListPresenter(this, this);
        req = new ArbitrationReq();
        initView();
    }

    private void initView() {
        final Toolbar toolbar = findViewById(R.id.toolbar);
        TextView tvTitle = findViewById(R.id.tv_title);
        toolbar.setNavigationIcon(R.mipmap.ic_back_left);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
                hideSoft(toolbar);
            }
        });
        tvTitle.setText("样本仲裁");
        refreshLayout = findViewById(R.id.refreshLayout);
        tvShaixuan = findViewById(R.id.tv_shaixuan);
        editSearch = findViewById(R.id.edit_search);
        recycler = findViewById(R.id.recycler);
        recycler.setLayoutManager(new LinearLayoutManager(this));
        recycler.setAdapter(adapter = new ZhongcaiAdapter(R.layout.item_zhongcai, null));
        View view = LayoutInflater.from(this).inflate(R.layout.layout_empty_list, (ViewGroup) recycler.getParent(), false);
        adapter.setEmptyView(view);
        adapter.isFirstOnly(false);
        adapter.openLoadAnimation();
        adapter.openLoadMore(10, true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadMore();
            }
        });
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                Player currentPlayer = adapter.getCurrentPlayer();
                if (currentPlayer != null) {
                    currentPlayer.stop();
                }
                hideSoft(recycler);
                req.setKeyWord(editSearch.getText().toString().trim());
                refresh();
            }
        });
        adapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (!AppUtil.isFastClick()) {
                    ArbitrationBean.ResultBean resultBean = adapter.getData().get(position);
                    Intent intent = new Intent(ZhongcaiListActivity.this, ShLabelDetailActivity.class);
                    if (resultBean.getSample() == null || resultBean.getSampleDetail() == null) {
                        showToast("有损数据");
                        return;
                    }
                    intent.putExtra("type", 2);
                    intent.putExtra("id", resultBean.getId());
                    intent.putExtra("sampleId", resultBean.getSampleId());
                    intent.putExtra("labelId", resultBean.getSample().getLabelId());
                    intent.putExtra("info", resultBean.getSampleDetail());
                    intent.putExtra("status", resultBean.getStatus());
                    startActivity(intent);
                }
            }
        });
        editSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            //发送请求
                            req.setKeyWord(v.getText().toString().trim());
                            refresh();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }
        });
        initDrawer();
        refresh();
    }

    private void initDrawer() {
        shaixuanBeanList.add(new ShaixuanBean(false, "未仲裁", "10"));
        shaixuanBeanList.add(new ShaixuanBean(false, "已仲裁", "20"));
        drawer = findViewById(R.id.drawer);
        rcCondition = findViewById(R.id.rc_condition);
        btnChongzhi = findViewById(R.id.btn_chongzhi);
        btnConfirm = findViewById(R.id.btn_confirm);
        rcCondition.setLayoutManager(new GridLayoutManager(this, 3));
        rcCondition.setAdapter(shaixuanAdapter = new ShaixuanAdapter(R.layout.item_shaixuan, shaixuanBeanList));
        btnConfirm.setOnClickListener(this);
        btnChongzhi.setOnClickListener(this);
        shaixuanAdapter.setOnRecyclerViewItemChildClickListener(new BaseQuickAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                List<ShaixuanBean> data = adapter.getData();
                for (int i = 0; i < data.size(); i++) {
                    data.get(i).setCheck(false);
                }
                data.get(position).setCheck(true);
                adapter.notifyDataSetChanged();
                req.setStatus(data.get(position).getStatus());
            }
        });
        tvShaixuan.setOnClickListener(this);
        btnChongzhi.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
    }


    private void loadMore() {
        req.setPageNo(1 + req.getPageNo());
        presenter.getList(req, refreshLayout);
    }

    private void refresh() {
        req.setPageNo(1);
        presenter.getList(req, refreshLayout);
    }

    @Override
    public void getListSuc(ArbitrationBean bean) {
        if (req.getPageNo() == 1) {
            refreshLayout.setRefreshing(false);
            adapter.setNewData(bean.getResult());
        } else {
            adapter.addData(bean.getResult());
        }
        adapter.openLoadMore(bean.getTotalItems() > adapter.getItemCount());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_shaixuan:
                drawer.openDrawer(Gravity.RIGHT);
                break;
            case R.id.btn_chongzhi:
                List<ShaixuanBean> shaixuanList = shaixuanAdapter.getData();
                for (int i = 0; i < shaixuanList.size(); i++) {
                    shaixuanList.get(i).setCheck(false);
                }
                shaixuanAdapter.notifyDataSetChanged();
                drawer.closeDrawer(Gravity.RIGHT);
                editSearch.setText("");
                req.setStatus("");
                req.setKeyWord("");
                refresh();
                break;
            case R.id.btn_confirm:
                drawer.closeDrawer(Gravity.RIGHT);
                editSearch.setText("");
                req.setKeyWord("");
                refresh();
                break;
        }
    }

    @Override
    public void onError(String code, String msg) {
        super.onError(code, msg);
        showToast(msg);
        refreshLayout.setRefreshing(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Player currentPlayer = adapter.getCurrentPlayer();
        if (currentPlayer != null) {
            currentPlayer.stop();
        }
    }
}
